package lancs.mobilemedia.core.ui.controller;

import java.util.Vector;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import lancs.mobilemedia.core.ui.MainUIMidlet;
import lancs.mobilemedia.core.ui.datamodel.AlbumData;
import lancs.mobilemedia.core.ui.datamodel.ImageData;
import lancs.mobilemedia.core.ui.screens.AlbumListScreen;
import lancs.mobilemedia.core.ui.screens.NewLabelScreen;
import lancs.mobilemedia.core.ui.screens.PhotoViewScreen;
import lancs.mobilemedia.core.util.Constants;
import lancs.mobilemedia.factories.CopyPhotoSPLFactory;
import lancs.mobilemedia.factories.FavouritesSPLFactory;
import lancs.mobilemedia.factories.SPLFactory;
import lancs.mobilemedia.factories.SortingSPLFactory;
import lancs.mobilemedia.lib.exceptions.ImageNotFoundException;
import lancs.mobilemedia.lib.exceptions.InvalidImageDataException;
import lancs.mobilemedia.lib.exceptions.PersistenceMechanismException;

public class PhotoController extends PhotoListController {

	private ImageData image;
	private NewLabelScreen screen;

	private ControllerCommand command;
	private ControllerMapper photoControllerMapper;

	private Vector photoControllerFactories;

	private ShowImageController showImageController;

	public PhotoController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) {
		super(midlet, albumData, albumListScreen);
		init();
	}

	public void init() {
		createFactories();
		createControllerMapper();
		createShowImageController();
	}

	private void createFactories() {
		photoControllerFactories = new Vector();
		// if Sorting feature is enabled
		photoControllerFactories.addElement(new SortingSPLFactory());
		// if Favourites feature is enable
		photoControllerFactories.addElement(new FavouritesSPLFactory());
		// if CopyPhoto feature is enable
		photoControllerFactories.addElement(new CopyPhotoSPLFactory());
	}

	private void createShowImageController() {
		showImageController = new DefaultShowImageController();
		SPLFactory factory;
		for (int i = 0; i < photoControllerFactories.size(); i++) {
			factory = (SPLFactory) photoControllerFactories.elementAt(i);
			showImageController = factory.createShowImageController(showImageController);
		}
	}

	public void createControllerMapper() {
		photoControllerMapper = new PhotoControllerMapper();
		SPLFactory factory;
		for (int i = 0; i < photoControllerFactories.size(); i++) {
			factory = (SPLFactory) photoControllerFactories.elementAt(i);
			photoControllerMapper = factory.createControllerMapper(photoControllerMapper);
		}
	}

	public boolean handleCommand(Command c) {
		String label = c.getLabel();
		System.out.println(this.getClass().getName() + "::handleCommand: " + label);

		command = (ControllerCommand) photoControllerMapper.getCommand(label);

		if (command != null) {
			return command.execute(this);
		}
		return false;
	}

	public void updateImage(ImageData image) {
		try {
			getAlbumData().updateImageInfo(image, image);
		} catch (InvalidImageDataException e) {
			e.printStackTrace();
		} catch (PersistenceMechanismException e) {
			e.printStackTrace();
		}
	}

	public String getSelectedImageName() {
		List selected = (List) Display.getDisplay(midlet).getCurrent();
		if (selected == null)
			System.out.println("Current List from display is NULL!");
		String name = selected.getString(selected.getSelectedIndex());
		return name;
	}

	public void showImage(String imageName) {
		showImageController.showImage(this, imageName);
	}

	public boolean goToPreviousScreen() {
		System.out.println("<* PhotoController.goToPreviousScreen() *>");
		(new TracerController()).sendInformation("<* PhotoViewController.handleCommand() *> ");
		String currentScreenName = ScreenSingleton.getInstance().getCurrentScreenName();
		if (currentScreenName.equals(Constants.IMAGELIST_SCREEN)) {
			getAlbumListScreen().repaintListAlbum(getAlbumData().getAlbumNames());
			setCurrentScreen(getAlbumListScreen());
			ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN);
			return true;
		} else if (currentScreenName.equals(Constants.IMAGE_SCREEN)) {
			showImageList(getCurrentStoreName(), false, false);
			ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
			return true;
		} else if (currentScreenName.equals(Constants.ADDPHOTOTOALBUM_SCREEN)) {
			showImageList(getCurrentStoreName(), false, false);
			ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
			return true;
		}
		return false;
	}

	public void setImage(ImageData image) {
		this.image = image;
	}

	public ImageData getImage() {
		return image;
	}

	public void setScreen(NewLabelScreen screen) {
		this.screen = screen;
	}

	public NewLabelScreen getScreen() {
		return screen;
	}
}
